﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ScarfMaker.Dal;
using System.IO;

namespace ScarfMaker
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }
        public void _init()
        {
            this.DialogResult = false;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            tbUserName.Focusable = true;
            tbUserName.Focus();
        }
        private void login_Click(object sender, RoutedEventArgs e)
        {
            ToLogin();
        }
        void ToLogin()
        {
            string username = tbUserName.Text.Trim();
            string password = tbPassWord.Password;
            if (DataBase.ValidateUser(username, password))
            {
                Models.StaticClass.UserName = username;
                Models.StaticClass.PassWord = password;
                if (!CheckPath())
                {
                    return;
                }
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误,请重新登录");
            }
        }
        public static bool CheckPath()
        {
            if (Models.StaticClass.diScarfData == null || !Models.StaticClass.diScarfData.Exists)
            {
                GetScarfDirector();
            }
            if (Models.StaticClass.diScarfData == null || !Models.StaticClass.diScarfData.Exists)
            {
                return false;
            }
            else
            {
                DirectoryInfo diTuAn = Models.StaticClass.diScarfData.GetDirectories("图案").FirstOrDefault();
                DirectoryInfo diDiWen = Models.StaticClass.diScarfData.GetDirectories("底纹").FirstOrDefault();
                DirectoryInfo diBianKuang = Models.StaticClass.diScarfData.GetDirectories("边框").FirstOrDefault();
                if (diTuAn == null | diDiWen == null || diBianKuang == null)
                {
                    MessageBox.Show("目录结构错误,请重新选择包好<底纹><图形><边框>子目录的目录");
                    return false;
                }
                return true;
            }
        }

        private void tips_close_MouseEnter(object sender, MouseEventArgs e)
        {
            Label lb_1 = (Label)sender;
            try
            {
                ImageBrush ib1 = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Images/cancel_1.png")));

                ib1.Stretch = Stretch.Fill;


                lb_1.Background = ib1;
            }
            catch (Exception ef)
            {
                MessageBox.Show("出现错误！：" + ef.ToString());
            }

        }

        private void tips_close_MouseLeave(object sender, MouseEventArgs e)
        {
            Label lb_1 = (Label)sender;
            try
            {
                ImageBrush ib1 = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "Images/cancel.png")));

                ib1.Stretch = Stretch.Fill;


                lb_1.Background = ib1;
            }
            catch (Exception ef)
            {
                MessageBox.Show("出现错误！：" + ef.ToString());
            }
        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btn_login_MouseEnter(object sender, MouseEventArgs e)
        {
            Button btn_login = (Button)sender;
            Label lb1 = (Label)btn_login.Template.FindName("tips_for_login", btn_login);
            lb1.Foreground = new SolidColorBrush(Colors.Red);

        }

        private void btn_login_MouseLeave(object sender, MouseEventArgs e)
        {
            Button btn_login = (Button)sender;
            Label lb1 = (Label)btn_login.Template.FindName("tips_for_login", btn_login);
            lb1.Foreground = new SolidColorBrush(Colors.White);

        }

        private void tbUser_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key==Key.Enter)
            {
                ToLogin();
            }else if(e.Key==Key.Escape)
            {
                Application.Current.Shutdown(0);
            }
        }

        private void btnSetting_Click(object sender, RoutedEventArgs e)
        {
            Models.StaticClass.diScarfData = null;
            if(CheckPath())
            {
                lblMsg.Content = "方巾图案设计系统(已经设置)";
                lblMsg.ToolTip = Models.StaticClass.diScarfData.Name;
            }
        }  
        public static void GetScarfDirector()
        {

            System.Windows.Forms.FolderBrowserDialog m_Dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = m_Dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string m_Dir = m_Dialog.SelectedPath.Trim();
            Models.StaticClass.diScarfData = new DirectoryInfo(m_Dir);
        }
    }
}
